package com.zhipu.resetbalance.ui.home.system.inspector;

import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ActivityUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupAnimation;
import com.zhipu.resetbalance.R;
import com.zhipu.resetbalance.databinding.ActivityInspectorBinding;
import com.zhipu.resetbalance.listener.ChooseClickCallback;
import com.zhipu.resetbalance.mobile.dao.InspectorEntityDao;
import com.zhipu.resetbalance.mobile.event.InspectorEntity;
import com.zhipu.resetbalance.pop.SelectModifyDeletePopup;
import com.zhipu.resetbalance.ui.BaseActivity;
import com.zhipu.resetbalance.ui.home.system.inspector.adapter.InspectorAdapter;

import java.util.List;

/**
 * 人员
 */
public class InspectorActivity extends BaseActivity<ActivityInspectorBinding> implements ChooseClickCallback {

    /**
     * VM
     */
    private InspectorViewModel mInspectorViewModel;
    /**
     * 适配器
     */
    private InspectorAdapter mInspectorAdapter;

    /**
     *
     */
    private List<InspectorEntity> mInspectorEntityList;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_inspector;

    }

    @Override
    public void initViewModel() {
        super.initViewModel();
        //VM
        mInspectorViewModel = new ViewModelProvider(getViewModelStore(), new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(InspectorViewModel.class);
        binding.setViewModel(mInspectorViewModel);


    }

    @Override
    public void initView() {
        super.initView();
        mInspectorAdapter = new InspectorAdapter();
        mInspectorAdapter.setChooseClickCallback(this);
        binding.rv.setAdapter(mInspectorAdapter);
        binding.rv.setLayoutManager(new LinearLayoutManager(mContext));
    }


    @Override
    public void initData() {
        super.initData();
        if(null != mInspectorAdapter){
            mInspectorEntityList = InspectorEntityDao.getInstance().getAllList();
            mInspectorAdapter.setInspectorEntityList(mInspectorEntityList);
        }

    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        mInspectorViewModel.inspector.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                initData();
            }
        });

    }

    @Override
    public void onChooseClickCallback(int mInt) {
        InspectorEntity mInspectorEntity = mInspectorEntityList.get(mInt);
        if(null != mInspectorEntity){
            SelectModifyDeletePopup mSelectModifyDeletePopup = new SelectModifyDeletePopup(mContext,true);
            mSelectModifyDeletePopup.setChooseClickCallback(new ChooseClickCallback() {
                @Override
                public void onChooseClickCallback(int mInt) {
                    switch (mInt){
                        case 1:
                            if(null != mInspectorViewModel){
                                mInspectorViewModel.AddPersonnel(mInspectorEntity);
                            }
                            break;
                        case 2:
                            if(mInspectorEntity.delete()){
                                //刷新页面
                                initData();
                            }
                            break;
                    }
                }
            });
            new XPopup
                    .Builder(ActivityUtils.getTopActivity())
                    .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                    .asCustom(mSelectModifyDeletePopup)
                    .show();
        }
    }
}
